const db = uniCloud.database()
const dbOrder = db.collection("t_shop_order")
const dbUser = db.collection("uni-id-users")
const dbOrderItem = db.collection("t_shop_order_item")
const dbOrderLog = db.collection("t_shop_order_log")
const dbGoods = db.collection("t_shop_goods")
const dbCmd = db.command
module.exports = {
	_before: function () { // 通用预处理器

	},
   
   getOrders(status, limit, page, idUser) {
	   let res = dbOrder
	   .where({
		   status: dbCmd.eq(status),
		   id_user: idUser
	   })
	   .skip((page-1)*limit).limit(limit).get()
	   return res
   },
   cancelOrder(orderSn) {
	   dbOrder.where({order_sn: dbCmd.eq(orderSn)}).remove()
   },
   getByOrderSn(orderSn) {
	   let res = dbOrder.where({order_sn: dbCmd.eq(orderSn)}).get()
	   return res
   },
   updateOrder(idOrder, status) {
	   dbOrder.where({_id: dbCmd.eq(idOrder)}).update({status: status})
   },
   
   getUser(id) {
	   let res = dbUser.where({_id: dbCmd.eq(id)}).get()
	   return res
   },
   
   getOrderItems(idOrder) {
	   let res = dbOrderItem.where({id_order: dbCmd.eq(idOrder)}).get()
	   return res
   },
   
   saveOrderLog(idOrder, descript) {
	   dbOrderLog.add({
		   descript: descript,
		   idOrder: idOrder
	   })
   },
   getGoodsById(idGoods) {
	   let res = dbGoods.where({_id: dbCmd.eq(idGoods)}).get()
	   return res
   }
   // TODO: getCategoryById
}
